package com.aaa.qy127.service.impl;

import com.aaa.qy127.dao.GWCDao;
import com.aaa.qy127.entity.Dl;
import com.aaa.qy127.service.GWCService;
import com.aaa.qy127.util.CodeAndMsg;
import com.aaa.qy127.util.Result;
import com.sun.org.apache.bcel.internal.classfile.Code;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service("gwcServiceImpl")
public class GWCServiceImpl implements GWCService {

    /**
     * 注入 GWCDao
     */
    @Resource(name = "gwcDaoImpl")
    private GWCDao gwcDao;

    /**
     * 插入 一条数据
     * @param bookid
     * @param count
     * @param outprice
     * @param request
     * @return
     */
    @Override
    public CodeAndMsg insertGwc(String bookid, String count, String outprice, HttpServletRequest request) {
        Dl dl = (Dl)request.getSession().getAttribute("dl");
        //System.out.println(dl);
        CodeAndMsg codeAndMsg = new CodeAndMsg();
        if (dl ==null){
            codeAndMsg.setCode(1003);
        }else {
            int i = gwcDao.insertOneGwc(bookid, count, outprice, dl.getUserid());
            if (i>0){
                codeAndMsg.setCode(1001);
                codeAndMsg.setMsg("添加成功");
            }else {
                codeAndMsg.setCode(1002);
                codeAndMsg.setMsg("添加失败");
            }
        }
        return codeAndMsg;
    }

    /**
     * 查询到 所有的 购物车 信息 并 返回到 前端 页面
     * @return
     */
    @Override
    public List<Map<String,Object>> selectAllGwc(HttpServletRequest request) {
        Dl dl = (Dl)request.getSession().getAttribute("dl");
        List<Map<String, Object>> allGWC = gwcDao.selectAllGWC(dl.getUserid());
        //System.out.println(allGWC);
        request.getSession().setAttribute("allGWC",allGWC);
        return allGWC;
    }

    /**
     * 通过 gwcid  修改 数量之后 在 查询 一次 全部信息
     * @param gwcid
     * @param count
     * @param request
     * @return
     */
    @Override
    public List<Map<String, Object>> updatecountByGwcid(String gwcid, String count, HttpServletRequest request) {
        int i = gwcDao.updatecountByGwcid(gwcid, count);
        if (i>0){
            Dl dl = (Dl)request.getSession().getAttribute("dl");
            List<Map<String, Object>> allGWC = gwcDao.selectAllGWC(dl.getUserid());
            request.getSession().setAttribute("allGWC",allGWC);
            return allGWC;
        }
        return null;
    }


    /**
     * 通过 gwcid 删除 一条数据之后 再查询一次 所有信息 并返回到 页面
     * @return
     */
    @Override
    public List<Map<String, Object>> deleteGwcByGwcid(String gwcid,HttpServletRequest request) {
        int i = gwcDao.deleteGwcByGwcid(gwcid);
        if (i>0){
            Dl dl = (Dl)request.getSession().getAttribute("dl");
            List<Map<String, Object>> allGWC = gwcDao.selectAllGWC(dl.getUserid());
            request.getSession().setAttribute("allGWC",allGWC);
            return allGWC;
        }
        return null;
    }
}
